package com.yanqu.road.server.cmd.specialcookboy;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.specialcookboy.SpecialCookBoyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.specialcookboy.SpecialCookBoyModule;
import com.yanqu.road.server.manger.specialcookboy.SpecialCookBoyMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_SPECIAL_COOK_BOY_USE_SHARE_CODE_RESULT_FROM_CROSS, desc = "小当家特色服使用分享码跨服返回结果")
public class SpecialCookBoyUseCodeRespFormCorssCmd extends AbstractCommand {

    private int ret;
    private String shareCode;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SpecialCookBoyProto.SpecialCookBoyUseShareCodeResultFromCross req = SpecialCookBoyProto.SpecialCookBoyUseShareCodeResultFromCross.parseFrom(packet.getBytes());
        ret = req.getRet();
        shareCode = req.getCode();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (ret == 0) { // 跨服返回成功，本服再使用分享码,不成功直接走下面返回
            ret = player.getModule(SpecialCookBoyModule.class).dealUseCode(shareCode);
            player.getModule(SpecialCookBoyModule.class).syncUserData();
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0){
            SpecialCookBoyProto.SpecialCookBoyUseShareCodeResp.Builder builder = SpecialCookBoyProto.SpecialCookBoyUseShareCodeResp.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_SPECIAL_COOK_BOY_USE_SHARE_CODE,builder);
        }

    }
}
